package com.test.admin.controller;

import com.test.admin.pojo.Script;
import com.test.admin.pojo.TableData;
import com.test.admin.quartz.QuartzManager;
import com.test.admin.service.ScriptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("script.html")
public class ScriptController {
    @Autowired
    private ScriptService scriptService;

    @RequestMapping
    public String page() {
        return "scripts";
    }

    @RequiresPermissions("sys:scripts:list")
    @RequestMapping(params = "act=table")
    @ResponseBody
    public TableData table(Script script) {
        List<Script> list = scriptService.getScriptList(script);
        return new TableData(list.size(), list);
    }

    @RequiresPermissions("sys:scripts:add")
    @RequestMapping(params = "act=add")
    @ResponseBody
    public Map<String, Object> add(Script script) {
        Map<String, Object> result = new HashMap<>();
        try {
            scriptService.addScript(script);
            result.put("status", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("sys:scripts:update")
    @RequestMapping(params = "act=update")
    @ResponseBody
    public Map<String, Object> update(Script script) {
        Map<String, Object> result = new HashMap<>();
        try {
            scriptService.updateScript(script);
            result.put("status", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequiresPermissions("sys:scripts:update")
    @RequestMapping(params = "act=resume")
    @ResponseBody
    public Map<String, Object> resume(Integer id) {
        Map<String, Object> result = new HashMap<>();
        scriptService.doResumeJob(id);
        result.put("status", true);
        return result;
    }
    @RequiresPermissions("sys:scripts:pause")
    @RequestMapping(params = "act=pause")
    @ResponseBody
    public Map<String, Object> pause(Integer id) {
        Map<String, Object> result = new HashMap<>();
        scriptService.doPauseJob(id);
        result.put("status", true);
        return result;
    }

}
